﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;

namespace WebC
{
    public partial class base_company : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(base_company));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        [Ajax.AjaxMethod]
        public bool deleFile(string sFileName)
        {
            return deleteFileByPath(sFileName);
        }

        #region 载入信息

        public void loadInfo()
        {
            BaseInfo baseinfo = null;
            string sModelName = "";
            DataTable dt = null;
            try
            {
                baseinfo = new BaseInfo();
                if (m_Id == 0)
                {
                    if (!checkModelRightById(523, ref sModelName, (clsBaseLib)baseinfo))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(522, ref sModelName, (clsBaseLib)baseinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(525, ref sModelName, (clsBaseLib)baseinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    dt = baseinfo.getCompanyInfoById(m_Id);
  
                }
                sModelFullName = sModelName;
            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (dt.Rows.Count == 1)
                {
                    txtCompanyName.Text = Convert.ToString(dt.Rows[0]["Companyname"]);
                    txtCompanyDesc.Text = Convert.ToString(dt.Rows[0]["Companydesc"]);
                    txtAddress.Text = Convert.ToString(dt.Rows[0]["address"]);
                    txtFaxNum.Text = Convert.ToString(dt.Rows[0]["faxnum"]);
                    txtTeleNum.Text = Convert.ToString(dt.Rows[0]["telenum"]);
                    txtContracter.Text = Convert.ToString(dt.Rows[0]["contracter"]);
                    ddlCompanyType.SelectedValue = Convert.ToString(dt.Rows[0]["companytype"]);
                    tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["filehtml"]);
                    txtPostNum.Text = Convert.ToString(dt.Rows[0]["postnum"]);
                }
            }
        }


        #endregion

        public string sFileName = "companyfile";


        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int iCompanyId, string sName,int iCompanyType,string sContracter, string sMobileNum, string sTele, string sFaxNum
            ,string sAddress, string sPostNum, string sFileHtml, string sDesc)
        {
            BaseInfo baseinfo = null;
            bool bResult = false;
            sName = sName.Replace("'", "''");
            sContracter = sContracter.Replace("'", "''");
            sTele = sTele.Replace("'", "''");
            sFaxNum = sFaxNum.Replace("'", "''");
            sAddress = sAddress.Replace("'", "''");
            sPostNum = sPostNum.Replace("'", "''");
            sFileHtml = sFileHtml.Replace("'", "''");
            sDesc = sDesc.Replace("'", "''");
            try
            {
                baseinfo = new BaseInfo();
                bResult = baseinfo.setCompanyInfo(iCompanyId, sName, iCompanyType, sContracter, sMobileNum, sTele, sFaxNum, sAddress, sPostNum, sFileHtml, sDesc, iUserId);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}